<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\BaseController;
use app\common\model\system\Statement;
use app\common\model\system\Menu;
use app\common\model\system\Statement as StateModel;
use think\Exception;


class Index extends BaseController
{
    //中间件
    protected $middleware = [
        'method:ajax' => ['only'=>['browserList','pageviewList','locationList','unlock','visitedList']],
    ];

    //后台首页
    public function index()
    {
        return view('index',[
            'appName' => config('app.app_name'),
            'user' => app()->user->getIdentity()
        ]);
    }

    //控制台
    public function console(){
        return view('console',[
            'counts' => Statement::getCounts(),
        ]);
    }

    //访问记录
    public function visitedList(){
        try{
            //获取列表数据
            $data = StateModel::getStatementListData();


        }catch (\Exception $e){
            //系统错误
            //Log::error($e->getMessage());
            return json([
                'code' => 1,
                'msg' => $e->getMessage(),
            ]);
        }

        //成功返回
        return json([
            'code'  => 0,
            'msg'=> '查询列表成功',
            'count' => $data['count'],
            'data'  => $data['data']
        ]);
    }

    //浏览器记录
    public function browserList(){
        try{
            //获取列表数据
            $data = Statement::getBrowserListData();


        }catch (\Exception $e){
            //系统错误
            //Log::error($e->getMessage());
            return json([
                'code' => 1,
                'msg' => $e->getMessage(),
            ]);
        }

        //成功返回
        return json([
            'code'  => 0,
            'msg'=> '查询列表成功',
            'data'  => $data
        ]);
    }

    //访问量
    public function pageviewList(){
        try{
            //获取列表数据
            $data = Statement::getPageview();


        }catch (\Exception $e){
            //系统错误
            //Log::error($e->getMessage());
            return json([
                'code' => 1,
                'msg' => $e->getMessage(),
            ]);
        }

        //成功返回
        return json([
            'code'  => 0,
            'msg'=> '查询列表成功',
            'data'  => $data
        ]);
    }

    //用户分布
    public function locationList(){
        try{
            //获取列表数据
            $data = Statement::userLocation();


        }catch (\Exception $e){
            //系统错误
            //Log::error($e->getMessage());
            return json([
                'code' => 1,
                'msg' => $e->getMessage(),
            ]);
        }

        //成功返回
        return json([
            'code'  => 0,
            'msg'=> '查询列表成功',
            'data'  => $data
        ]);
    }

    //获取后台菜单
    public function menus(){
        try{
            return json(Menu::getAdminNavs());
            //return $this->navs();
        }catch (\Exception $e){
            return json([
                'code' => 1,
                'msg' => '请求菜单失败'
            ]);
        }

    }

    //锁屏解锁
    public function unlock(string $password){
        try{
            $model = app()->user->getIdentity();
            if(!$model->validatePassword($password)){
                throw new Exception('密码错误.');
            }

            return json(['code'=>0,'msg'=>'密码正确']);

        }catch (\Exception $e){
            return json([
                'code' => 1,
                'msg' => $e->getMessage()
            ]);
        }
    }

    /**
     * 测试数据
     * @return string
     */
    private function navs(){
        return <<<NAVS
[
  {
    "title": "控制台",
    "href": "index/console",
    "fontFamily": "ok-icon",
    "icon": "&#xe654;",
    "spread": true,
    "isCheck": true
  },
  {
    "title": "内容管理",
    "href": "",
    "fontFamily": "ok-icon",
    "icon": "&#xe723;",
    "children": [
      {
        "title": "分类管理",
        "href": "/index.php/admin/category/index",
        "icon": "&#xe62e;"
      },{
        "title": "话题管理",
        "href": "/index.php/admin/topic/index",
        "icon": "&#xe62e;"
      },{
        "title": "文章管理",
        "href": "/index.php/admin/article/index",
        "icon": "&#xe62e;"
      },{
        "title": "标签管理",
        "href": "/index.php/admin/tag/index",
        "icon": "&#xe62e;"
      }
    ]
  },
  {
    "title": "用户管理",
    "href": "",
    "fontFamily": "ok-icon",
    "icon": "&#xe723;",
    "children": [
      {
        "title": "用户管理",
        "href": "/index.php/admin/user/index",
        "icon": "&#xe62e;"
      },{
        "title": "权限管理",
        "href": "/index.php/admin/permission/index",
        "icon": "&#xe62e;"
      }
    ]
  },
  {
    "title": "消息管理",
    "href": "",
    "fontFamily": "ok-icon",
    "icon": "&#xe723;",
    "children": [
      {
        "title": "评论管理",
        "href": "/index.php/admin/comment/index",
        "icon": "&#xe62e;"
      },{
        "title": "留言管理",
        "href": "/index.php/admin/message/index",
        "icon": "&#xe62e;"
      }
    ]
  },
  {
    "title": "站点管理",
    "href": "",
    "fontFamily": "ok-icon",
    "icon": "&#xe723;",
    "children": [
      {
        "title": "日志管理",
        "href": "/index.php/admin/log/index",
        "icon": "&#xe62e;"
      },{
        "title": "站点设置",
        "href": "/index.php/admin/setting/index",
        "icon": "&#xe62e;"
      },{
        "title": "菜单管理",
        "href": "/index.php/admin/menu/index",
        "icon": "&#xe62e;"
      },{
        "title": "访问记录",
        "href": "/index.php/admin/statement/index",
        "icon": "&#xe62e;"
      }

    ]
  },
  {
    "title": "数据管理",
    "href": "",
    "fontFamily": "ok-icon",
    "icon": "&#xe723;",
    "children": [
      {
        "title": "数据清理",
        "href": "/index.php/admin/garbage/index",
        "icon": "&#xe62e;"
      },{
        "title": "备份还原?",
        "href": "/index.php/admin/menu/index",
        "icon": "&#xe62e;"
      }

    ]
  }
]
NAVS;
    }

}
